package com.commerceE.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.commerceE.dao.ProductDao;
import com.commerceE.dao.ProductInstanceDao;
import com.commerceE.dao.ProductOptionValueDao;
import com.commerceE.domain.Product_instance;
import com.commerceE.domain.Product_option_values;
import com.commerceE.domain.Products;
import com.commerceE.service.ImageService;
import com.commerceE.service.ProductInstanceService;

@Transactional
public class ProductInstanceServiceImpl implements ProductInstanceService{
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private ProductInstanceDao productInstanceDao;
	
	@Autowired
	private ProductOptionValueDao productOptionValueDao;
	
	@Autowired
	private ImageService imageService;
	
	public List<Product_instance> getProductInstanceByProductId(Long productId){
		List<Product_instance> productInstanceList = productInstanceDao.getProductInstanceByProductId(productId);
		for(Product_instance productInstance : productInstanceList){
			Product_option_values color_product_option_value = productOptionValueDao.get(productInstance.getColorId());
			Product_option_values size_product_option_value = productOptionValueDao.get(productInstance.getSizeId());
			productInstance.setColor_product_option_value(color_product_option_value);
			productInstance.setSize_product_option_value(size_product_option_value);
			//Get product information
			productInstance.setProduct(productDao.get(productInstance.getProductId()));
		}
		return productInstanceList;
	}
	
	public Product_instance getProduct_instance(Long productInstanceId){
		Product_instance productInstance = productInstanceDao.get(productInstanceId);
		Product_option_values color_product_option_value = productOptionValueDao.get(productInstance.getColorId());
		Product_option_values size_product_option_value = productOptionValueDao.get(productInstance.getSizeId());
		productInstance.setColor_product_option_value(color_product_option_value);
		productInstance.setSize_product_option_value(size_product_option_value);
		productInstance.setProduct(productDao.get(productInstance.getProductId()));
		return productInstance;
	}
	
	public Product_instance getProductInstanceByProductIdAndColorAndSize(Long productId, Long colorId,Long sizeId){
		Product_instance product_instance = productInstanceDao.getProductInstanceByProductIdAndColorAndSize(productId, 
				colorId, sizeId);
		if(product_instance!=null){
			Products product = productDao.get(product_instance.getProductId());
			product.setBestBuyImage(imageService.getProductBestBuyImage(product.getProductId()));
			product_instance.setProduct(product);
			Product_option_values color_product_option_value = productOptionValueDao.get(product_instance.getColorId());
			Product_option_values size_product_option_value = productOptionValueDao.get(product_instance.getSizeId());
			product_instance.setColor_product_option_value(color_product_option_value);
			product_instance.setSize_product_option_value(size_product_option_value);
			
			
		}
		 return product_instance;
		
	}
	
	public void insertProductInstance(Product_instance productInstance){
		productInstanceDao.insert(productInstance);
	}
	
	public void updateProductInstance(Product_instance productInstance){
		productInstanceDao.update(productInstance);
	}


	//setter and getter
	public ProductDao getProductDao() {
		return productDao;
	}


	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}


	public ProductInstanceDao getProductInstanceDao() {
		return productInstanceDao;
	}


	public void setProductInstanceDao(ProductInstanceDao productInstanceDao) {
		this.productInstanceDao = productInstanceDao;
	}


	public ProductOptionValueDao getProductOptionValueDao() {
		return productOptionValueDao;
	}


	public void setProductOptionValueDao(ProductOptionValueDao productOptionValueDao) {
		this.productOptionValueDao = productOptionValueDao;
	}

	public ImageService getImageService() {
		return imageService;
	}

	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}
	
	

}
